

package org.example.elmfront.controller;


import org.example.elmfront.entity.Order;
import org.example.elmfront.entity.OrderItem;
import org.example.elmfront.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @PostMapping("/create")
    public Long createOrder(@RequestBody Order order) {
        return orderService.createOrder(order);
    }


    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable Long id) {
        return orderService.getOrderById(id);
    }


    @PutMapping("/{id}/status")
    public void updateOrderStatus(@PathVariable Long id, @RequestParam Integer status) {
        orderService.updateOrderStatus(id, status);
    }


    @DeleteMapping("/{id}")
    public void deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
    }


    @PostMapping("/item")
    public void addOrderItem(@RequestBody OrderItem orderItem) {
        orderService.addOrderItem(orderItem);
    }


    @GetMapping("/{id}/items")
    public List<OrderItem> getOrderItemsByOrderId(@PathVariable Long id) {
        return orderService.getOrderItemsByOrderId(id);
    }

    @PostMapping("/confirm")
    public void confirmOrder(@RequestBody List<OrderItem> orderItems) {
        orderService.confirmOrder(orderItems);
    }


    @DeleteMapping("/cancel")
    public void cancelOrder(@RequestBody List<OrderItem> orderItems) {
        orderService.cancelOrder(orderItems);
    }

    @GetMapping("/count")
    public int countOrders() {
        return orderService.countOrders();
    }

    @PostMapping("/createFromItems")
    public Long createOrderFromItems(@RequestBody List<OrderItem> orderItems) {
        return orderService.createOrderFromItems(orderItems);
    }
}
